<?php

/**
 *  database driver base PDO
 */
class Database extends PDO
{
    
    public function __construct()
    {
        parent::__construct('mysql:host=localhost;dbname=mvc', 'root', 'root_access');   
    }
    
    /**
    *   insert
    * 
    */
    public function insert($data)
    {
        ksort($data);
        
        $fieldNames = implode('`, `', array_keys($data));
        $fieldValues = ':' . implode(', :', array_keys($data));
        
        $sth = $this->prepare("insert into $table (`$fieldNames`) values ($fieldValues)");
                                                                          
        foreach($data as $key => $value)
        {
            $sth->bindValue(":$key", $value);   
        }
        
        // execute
        $sth->execute();
    }
    
    /**
    *   update
    * 
    */
    public function update($table, $data, $where)
    {
        ksort($data);
        
        $fieldDetails = NULL;
        
        foreach($data as $key => $value)
        {
            $fieldDetails .= "`$key`=:$key,";
        }
        
        $fieldDetails = rtrim($fieldDetails, ','); 
        
        $sth = $this->prepare("update $table set $fieldDetails where $where");
        
        foreach($data as $key => $value)
        {
            $sth->bindValue(":$key", $value);   
        }
        
        $sth->execute();
          
    }
    
}